﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SilverlightPhoneDatabase;
using System.Collections;
using SilverlightPhoneDatabase.Exceptions;
using System.Resources;
using System.Windows.Resources;
using System.IO;
using System.IO.IsolatedStorage;

namespace SilverlightPhoneDatabaseTest
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

        }

        private void RunTests_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in ResultPanel.Children)
            {
                if (item is TextBlock)
                {
                    ((TextBlock)item).Foreground = new SolidColorBrush(Colors.White);
                }
            }
            NamedTablesLegacyTest();
            NamedTablesTest();
            CreateDBTest();
            CreateTableTest();
            SaveTest();
            SaveSingleTableTest();
            OpenTest();
            AddRangeTest();
            RemoveRangeTest();
            RemoveRangeConditionTest();
            SaveFailsTest();
            SaveWithEncryptionTest();
            OpenWithEncryptionTest();
            SelectConditionTest();
            LazyLoadingTest();
            AddRowToExistingTableTest();
            AddRowToExistingTableTestLazyLoad();
            DatabaseExists();
            CreateTableFromXML();
            AsynchSaveTest();
            TableVersionInNameTest();
            CancelChangesTest();

        }

        private void NamedTablesTest()
        {
            Database.DeleteDatabase("test");
            string name1 = "p1";
            string name2 = "p2";
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>(name1);
            
            if (db.Table<Person>(name1) != null)
            {
                if (db.DoesTableExists(typeof(Person), name1) && !db.DoesTableExists(typeof(Person), "z") && !db.DoesTableExists(typeof(Person)))
                {
                    db.CreateTable<Person>();

                    db.Table<Person>(name1).Add(NewRandomPerson());
                    db.CreateTable<Person>(name2);
                    db.Table<Person>(name2).Add(NewRandomPerson());
                    db.Table<Person>(name2).Add(NewRandomPerson());

                    db.Table<Person>().Add(NewRandomPerson());
                    db.Table<Person>().Add(NewRandomPerson());
                    db.Table<Person>().Add(NewRandomPerson());
                    db.Table<Person>().Add(NewRandomPerson());

                    if (db.Table<Person>(name1).Count() == 1 && db.Table<Person>(name2).Count() == 2 && db.Table<Person>().Count() == 4)
                    {
                        db.Save();
                        db = null;
                        db = Database.OpenDatabase("test");
                        if (db.Table<Person>(name1).Count() == 1 && db.Table<Person>(name2).Count() == 2)
                        {
                            db.Table<Person>(name1).RemoveRange(c => true);
                            if (db.Table<Person>(name1).Count == 0)
                            {
                                db.CancelChanges<Person>();
                                if (db.Table<Person>(name1).Count == 0)
                                {
                                    this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Green);
                                }
                                else
                                {
                                    this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Red);
                                }
                            }
                            else
                            {
                                this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Red);
                            }
                        }
                        else
                        {
                            this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Red);
                        }
                    }
                    else
                    {
                        this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Red);
                    }
                }
                else
                {
                    this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else
            {
                this.NamedTablesLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
            Database.DeleteDatabase("test");
        }

        private string GetContent(string resourceName)
        {
            string content = string.Empty;
            Uri uri = new Uri(resourceName, UriKind.Relative);
            StreamResourceInfo info = Application.GetResourceStream(uri);
            using (StreamReader reader = new StreamReader(info.Stream))
            {
                content = reader.ReadToEnd();
            }
            return content;
        }

        private void CreateOldDB()
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (var stream = new IsolatedStorageFileStream("olddb", FileMode.OpenOrCreate, store))
                {

                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(GetContent("olddb.xml"));
                        writer.Flush();
                    }
                }
                using (var stream = new IsolatedStorageFileStream("olddb.SilverlightPhoneDatabaseTest.Person", FileMode.OpenOrCreate, store))
                {

                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(GetContent("olddb.SilverlightPhoneDatabaseTest.Person.xml"));
                        writer.Flush();
                    }
                }
                using (var stream = new IsolatedStorageFileStream("olddb.SilverlightPhoneDatabaseTest.Phone", FileMode.OpenOrCreate, store))
                {

                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(GetContent("olddb.SilverlightPhoneDatabaseTest.Phone.xml"));
                        writer.Flush();
                    }
                }
            }
        }

        private void NamedTablesLegacyTest()
        {
            var name = "olddb";
            Database.DeleteDatabase(name);
            if (Database.DoesDatabaseExists(name))
            {
                throw new Exception("Cannot run named tables test");
            }
            CreateOldDB();
            var db = Database.OpenDatabase(name, string.Empty, false);
            if (db.Table<Person>().Count() == 20 && db.Table<Phone>().Count() == 10)
            {
                this.NamedTablesLegacyLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.NamedTablesLegacyLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

            Database.DeleteDatabase(name);
        }

        private void CreateDBTest()
        {
            var name = Guid.NewGuid().ToString();
            if (Database.CreateDatabase(name) != null)
            {
                this.CreateDatabaseTestLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.CreateDatabaseTestLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
            Database.DeleteDatabase(name);
        }

        private void DatabaseExists()
        {
            var name = Guid.NewGuid().ToString();
            if (Database.DoesDatabaseExists(name) == false)
            {
                var db = Database.CreateDatabase(name);
                db.Save();
                if (Database.DoesDatabaseExists(name) == true)
                {
                    this.DatabaseExistsLabel.Foreground = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    this.DatabaseExistsLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else
            {
                this.DatabaseExistsLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

            Database.DeleteDatabase(name);
        }

        private void CreateTableTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                db.Table<Person>().Add(NewPerson());
                if (db.Table<Person>().Count == 1)
                {
                    this.CreateTableTestLabel.Foreground = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    this.CreateTableTestLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else
            {
                this.CreateTableTestLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void SaveTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
                db.Save();
                this.SaveDBLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.SaveDBLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
        }

        private void SaveSingleTableTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
                db.Save();
                db = Database.OpenDatabase("test");
                if (db.Table<Person>().Count == 20)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                    db.Table<Person>().Save();
                    db = Database.OpenDatabase("test");
                    if (db.Table<Person>().Count == 21)
                    {
                        this.SaveSingleTableLabel.Foreground = new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        this.SaveSingleTableLabel.Foreground = new SolidColorBrush(Colors.Red);
                    }
                }
                else
                {
                    this.SaveSingleTableLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else
            {
                this.SaveSingleTableLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
        }


        private void SaveWithEncryptionTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test", "password");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
                db.Save();
                this.SaveDBEncryptionLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.SaveDBEncryptionLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
        }


        private void OpenWithEncryptionTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test", "password");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
            }

            db.CreateTable<Phone>();

            if (db.Table<Phone>() != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    db.Table<Phone>().Add(NewRandomPhone());
                }
            }
            db.Save();


            db = Database.OpenDatabase("test", "password");
            if (db.Tables.Count == 2 && db.Table<Person>().Count == 20 && db.Table<Phone>().Count == 10)
            {
                this.OpenDBEncryptionLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.OpenDBEncryptionLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }


        private void OpenTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
            }

            db.CreateTable<Phone>();

            if (db.Table<Phone>() != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    db.Table<Phone>().Add(NewRandomPhone());
                }
            }
            db.Save();


            db = Database.OpenDatabase("test");
            if (db.Tables.Count == 2 && db.Table<Person>().Count == 20 && db.Table<Phone>().Count == 10)
            {
                this.OpenDBLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.OpenDBLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void AddRangeTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();
            List<Person> list = new List<Person>();
            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    list.Add(NewRandomPerson());
                }
            }

            db.Table<Person>().AddRange(list);
            db.Save();

            db = Database.OpenDatabase("test");
            if (db.Tables.Count == 1 && db.Table<Person>().Count == 20)
            {
                this.AddRangeLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.AddRangeLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void RemoveRangeTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            Person person1 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 1, BirthDate = new DateTime(1900, 1, 1) };
            Person person2 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 2, BirthDate = new DateTime(1900, 1, 1) };
            Person person3 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 3, BirthDate = new DateTime(1900, 1, 1) };

            db.Table<Person>().Add(person1);
            db.Table<Person>().Add(person2);
            db.Table<Person>().Add(person3);

            db.Table<Person>().Remove(person2);

            db.Save();

            db = Database.OpenDatabase("test");
            if (db.Tables.Count == 1 && db.Table<Person>().Count == 2)
            {
                this.RemoveRangeLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.RemoveRangeLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void RemoveRangeConditionTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            Person person1 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 1, BirthDate = new DateTime(1900, 1, 1) };
            Person person2 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 2, BirthDate = new DateTime(1900, 1, 1) };
            Person person3 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 3, BirthDate = new DateTime(1900, 1, 1) };

            db.Table<Person>().Add(person1);
            db.Table<Person>().Add(person2);
            db.Table<Person>().Add(person3);


            db.Save();

            db = Database.OpenDatabase("test");
            db.Table<Person>().RemoveRange((person) => { return (person.Salary >= 2); });
            db.Save();


            if (db.Tables.Count == 1 && db.Table<Person>().Count == 1)
            {
                this.RemoveRangeViaConditionLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.RemoveRangeViaConditionLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void SelectConditionTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test");
            db.CreateTable<Person>();

            Person person1 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 1, BirthDate = new DateTime(1900, 1, 1) };
            Person person2 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 2, BirthDate = new DateTime(1900, 1, 1) };
            Person person3 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 3, BirthDate = new DateTime(1900, 1, 1) };

            db.Table<Person>().Add(person1);
            db.Table<Person>().Add(person2);
            db.Table<Person>().Add(person3);

            var query = (from onePerson in db.Table<Person>()
                         where onePerson.Salary == 2
                         select onePerson);



            if (query.Count() == 1)
            {
                this.LinqSelectTestLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.LinqSelectTestLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void LazyLoadingTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test", "password");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
            }

            db.CreateTable<Phone>();

            if (db.Table<Phone>() != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    db.Table<Phone>().Add(NewRandomPhone());
                }
            }
            db.Save();


            db = Database.OpenDatabase("test", "password", true);
            if (db.Tables.Count == 2)
            {
                if (((ICollection)db.Tables[0]).Count == 0 && ((ICollection)db.Tables[1]).Count == 0)
                {
                    if (db.Table<Person>().Count == 20 && db.Table<Phone>().Count == 10)
                    {
                        this.LazyLoadingTestLabel.Foreground = new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        this.LazyLoadingTestLabel.Foreground = new SolidColorBrush(Colors.Red);
                    }
                }
                else
                {
                    this.LazyLoadingTestLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }


        }

        private void SaveFailsTest()
        {
            Table<Person> table = new Table<Person>();

            Person person1 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 1, BirthDate = new DateTime(1900, 1, 1) };
            table.Add(person1);

            try
            {
                table.Save();
                this.SaveTableFailsLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
            catch (TableCannotBeSavedException)
            {
                this.SaveTableFailsLabel.Foreground = new SolidColorBrush(Colors.Green);
            }



        }

        private void CreateTableFromXML()
        {
            var name = Guid.NewGuid().ToString();
            Database db = Database.CreateDatabase(name);
            if (db != null)
            {
                string content = string.Empty;
                Uri uri = new Uri("test.xml", UriKind.Relative);
                StreamResourceInfo info = Application.GetResourceStream(uri);
                using (StreamReader reader = new StreamReader(info.Stream))
                {
                    content = reader.ReadToEnd();
                }
                db.CreateNewTable(content, typeof(Person));
                db.Save();
                db = null;
                db = Database.OpenDatabase(name);
                if (db.Table<Person>().Count == 2)
                {
                    this.CreateTableFromXMLLabel.Foreground = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    this.CreateTableFromXMLLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else
            {
                this.CreateTableFromXMLLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
            Database.DeleteDatabase(name);
        }

        private void AddRowToExistingTableTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test", "password");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
            }

            db.CreateTable<Phone>();

            if (db.Table<Phone>() != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    db.Table<Phone>().Add(NewRandomPhone());
                }
            }
            db.Save();


            db = Database.OpenDatabase("test", "password", true);
            db.Table<Person>().Add(NewRandomPerson());

            db.Save();
            db = Database.OpenDatabase("test", "password", true);
            if (db.Tables.Count == 2 && db.Table<Person>().Count == 21 && db.Table<Phone>().Count == 10)
            {
                this.AddRowToExistingTableTestLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.AddRowToExistingTableTestLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void AddRowToExistingTableTestLazyLoad()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test", "password");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
            }

            db.CreateTable<Phone>();

            if (db.Table<Phone>() != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    db.Table<Phone>().Add(NewRandomPhone());
                }
            }
            db.Save();


            db = Database.OpenDatabase("test", "password", true);
            db.Table<Person>().Add(NewRandomPerson());

            db.Save();
            db = Database.OpenDatabase("test", "password", true);
            if (db.Tables.Count == 2 && db.Table<Person>().Count == 21 && db.Table<Phone>().Count == 10)
            {
                this.AddRowToExistingTableTestLazyLoadLabel.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.AddRowToExistingTableTestLazyLoadLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

        }

        private void AsynchSaveTest()
        {
            Table<Person> table = new Table<Person>();

            Person person1 = new Person() { FirstName = "John ", LastName = "Doe ", Salary = 1, BirthDate = new DateTime(1900, 1, 1) };
            table.Add(person1);

            table.BeginSave((r) =>
                {
                    if (r.Error == null)
                    {
                        this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Red);
                    }
                    else
                    {
                        this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Green);
                    }

                    Database.DeleteDatabase("test");
                    Database db = Database.CreateDatabase("test");
                    db.CreateTable<Person>();

                    if (db.Table<Person>() != null)
                    {
                        for (int i = 0; i < 20; i++)
                        {
                            db.Table<Person>().Add(NewRandomPerson());
                        }
                    }

                    db.CreateTable<Phone>();

                    if (db.Table<Phone>() != null)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            db.Table<Phone>().Add(NewRandomPhone());
                        }
                    }
                    db.BeginSave((r1) =>
                        {
                            if (r1.Error != null)
                            {
                                this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Red);
                            }
                            else
                            {
                                this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Green);
                                db = Database.OpenDatabase("test");

                                if (db.Tables.Count == 2 && db.Table<Person>().Count == 20 && db.Table<Phone>().Count == 10)
                                {
                                    this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Green);
                                    db.Table<Person>().Add(NewRandomPerson());

                                    db.Table<Person>().BeginSave((r2) =>
                                        {
                                            db = Database.OpenDatabase("test");
                                            if (db.Tables.Count == 2 && db.Table<Person>().Count == 21 && db.Table<Phone>().Count == 10)
                                            {
                                                this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Green);
                                            }
                                            else
                                            {
                                                this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Red);
                                            }
                                            Database.DeleteDatabase("test");
                                            MessageBox.Show("Test completed", "Silverlight Phone Database", MessageBoxButton.OK);
                                        });
                                }
                                else
                                {
                                    this.AsynchronousSaveLabel.Foreground = new SolidColorBrush(Colors.Red);
                                    Database.DeleteDatabase("test");
                                    MessageBox.Show("Test completed", "Silverlight Phone Database", MessageBoxButton.OK);
                                }

                            }

                        });
                });




        }

        private void TableVersionInNameTest()
        {
            var name = Guid.NewGuid().ToString();
            try
            {
                var db = Database.CreateDatabase(name);
                if (db != null)
                {
                    db.CreateTable<DbVersion>();
                    db.Table<DbVersion>().Add(new DbVersion() { Version = "1" });
                    db.Save();
                    var savedDb = Database.OpenDatabase(name);
                    var version = savedDb.Table<DbVersion>().First().Version;
                    if (version == "1")
                    {
                        this.TableVersionInNameTestLabel.Foreground = new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        this.TableVersionInNameTestLabel.Foreground = new SolidColorBrush(Colors.Red);
                    }
                }
                else
                {
                    this.TableVersionInNameTestLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            catch (Exception)
            {
                this.TableVersionInNameTestLabel.Foreground = new SolidColorBrush(Colors.Red);
            }
            finally
            {
                Database.DeleteDatabase(name);
            }


        }

        private void CancelChangesTest()
        {
            Database.DeleteDatabase("test");
            Database db = Database.CreateDatabase("test", "password");
            db.CreateTable<Person>();

            if (db.Table<Person>() != null)
            {
                for (int i = 0; i < 20; i++)
                {
                    db.Table<Person>().Add(NewRandomPerson());
                }
            }

            db.Save();


            db = Database.OpenDatabase("test", "password", true);
            db.Table<Person>().Add(NewRandomPerson());

            db.CancelChanges<Person>();

            if (db.Table<Person>().Count == 20)
            {
                this.CancelTestLabel.Foreground = new SolidColorBrush(Colors.Green);

                db = Database.OpenDatabase("test", "password", false);
                db.Table<Person>().Add(NewRandomPerson());

                db.CancelChanges<Person>();

                if (db.Table<Person>().Count == 20)
                {
                    this.CancelTestLabel.Foreground = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    this.CancelTestLabel.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else
            {
                this.CancelTestLabel.Foreground = new SolidColorBrush(Colors.Red);
            }

            Database.DeleteDatabase("test");

        }

        private Person NewPerson()
        {
            return new Person() { FirstName = "John", LastName = "Doe", Salary = 100, BirthDate = new DateTime(1900, 1, 1) };
        }

        private Person NewRandomPerson()
        {
            Random rand = new Random();
            return new Person() { FirstName = "John " + rand.Next(), LastName = "Doe " + rand.Next(), Salary = rand.Next(), BirthDate = new DateTime(1900 + rand.Next(1, 99), 1, 1) };
        }

        private Phone NewRandomPhone()
        {
            Random rand = new Random();
            return new Phone() { PhoneNumber = rand.Next().ToString(), Extension = rand.Next().ToString() };
        }
    }
}